<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@ include file="../../../../header.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>UserRole</title>
	</head>
<body>
	<div class="mod-toolbar-top cf" style="margin-top: 10px;">
		<div class="fr" style="margin-top: 30px;">
			<a href="javascript:void(0);" class="ui-btn ui-btn-sp" id="btn-find">搜索</a>
			<a href="javascript:void();" class="ui-btn ui-btn-sp mrb" id="btn-add">新增</a> 
			<a href="javascript:void(0);" class="ui-btn" id="btn-record">刷新</a> 
		</div>
	</div>
	<div class="grid-wrap" align="left" style="margin-left: 10px;">
		<table id="grid1"></table>
	</div>
	<script type="text/javascript">
		function update() {
			alert("update");
			$.ajax({
				url : "update",
				dataType : "json",
				success : function(data) {
					
				},
				erro : function() {
					alert("爆炸了！");
				}

			});
		}
		function deletes() {
			alert("update");
			$.ajax({
				url : "delete",
				dataType : "json",
				success : function(data) {

				},
				erro : function() {
					alert("爆炸了！");
				}
			});
		}
		function clearGrid() {
			$("#grid").jqGrid('clearGridData');
		}
		//添加图标
		function operationFormat(cellValue, options, rowObject) {
			return '<div class="operating" data-id="' + options.rowId + 
			'"><span class="ui-icon ui-icon-pencil" title="编辑" data-toggle="modal" data-target="#myModal"></span>&nbsp&nbsp'
					+ '<span class="ui-icon ui-icon-trash" title="删除"></span>';
		}
		$(function() {
			$("#grid1").jqGrid(
					{
						url : "/listAll",
						datatype : "json",
						height : 400,
						width : 1155,
						shrinkToFit : false,
						autoScroll : false,
						rownumbers : true,
						rowNum : -1,
						cellEdit : true,
						cellsubmit : "clientArray",
						colNames : [ "操作", "用户编号", "用户名","角色编号","角色名","公司"],
						colModel : [{
							name : "operating",
							width : 70,
							formatter : operationFormat,
							align : "center",
							sortable : false
						}, {
							name : "user.usercode",
							index : "usercode",
							editable : false,
							sorttype : "int",
							align : "center"
						}, {
							name : 'user.username',
							edittype : "text",
							align : "center",
							sortable : false,
							width : 200
						}, {
							name : "role.rolecode",
							editable : false,
							sorttype : "int",
							align : "center"
						},{
							name : 'role.rolename',
							edittype : "text",
							align : "center",
							sortable : false,
							width : 200
						}, {
							name : "company.companyname",
							editable : false,
							sorttype : "int",
							align : "center"
						}],
						loadComplete : function() {
							loadover();
						},
						gridComplete : function() {
						},
						afterInsertRow : function() {
						},
						beforeEditCell : function(rowid, cellname, v, iRow,
								iCol) {
							lastrow = iRow;
							lastcell = iCol;
						}
					});
			initTableRow();
			initEvent();
		})
		//设置每行的高度
		function loadover() {
			var grid = $("#grid1");
			var ids = grid.getDataIDs();
			for (var i = 0; i < ids.length; i++) {
				grid.setRowData(ids[i], false, {
					height : 35
				});
			}
		}
		function initTableRow() {
			for (var i = 1; i <= 4; i++) {
				jQuery("#grid1").jqGrid('addRowData', i, {});
			}
			loadover();
		}
		//添加事件
		function initEvent() {
			$("#btn-add").click(function(a) {
				handle.operate("add");
			}) , $("#grid1").on("click", ".operating .ui-icon-pencil", function(a) {
				var rowid = $(this).parent().data("id");
				var rowdata=$("#grid1").jqGrid("getRowData",rowid);
				console.log($(this).parent().data());
				handle.operate("edit",rowid,rowdata);
			}), $("#grid1").on("click", ".operating .ui-icon-trash", function(a) {
				a.preventDefault();
			 	var rowid = $(this).parent().data("id"); 
			 	
				handle.del(rowid);
			}),$("#btn-record").click(function(a) {
				$("#grid1").jqGrid().trigger("reloadGrid");
			}),$("#btn-find").click(function(a){                     //点击查找按钮传递到后台
				$.ajax({
					url:encodeURI("user/setsession"),				//encodeURI可把字符串作为uri进行 编码
					type:"POST",
					success:function(data){
						alert(data);
					}
				});	
			})
		}
		handle = {
				operate: function(oper, rowid, rowdata) {
					var title;
					var data={};
					if ("add" == oper) {
						title = "为用户新增一个角色";
						data={
							operate: oper,
							callback: this.callback
						}
						this.dialog = $.dialog({
							title: title,
							data:data,
							content:"url:/addRoleDialog",
							width: 500,
							height: 250,
							max: false,
							min: false,
							cache: false,
							lock: true
						});
					} else {
						title = "修改角色名";
						data={
							operate:oper,
							rowdata:rowdata,
							rowid:rowid,
							callback:this.callback
						}
						this.dialog = $.dialog({
							title: title,
							data:data,
							content:"url:/editRoleDialog",
							width: 500,
							height: 250,
							max: false,
							min: false,
							cache: false,
							lock: true
						});
					}
				},
				del: function(rowId) {
					var rowdata=$("#grid1").jqGrid("getRowData",rowId);
					var companyname = rowdata["company.companyname"];
					var usercode = rowdata["user.usercode"];
					var rolecode = rowdata["role.rolecode"];
					$.dialog.confirm("删除的角色将不能恢复，请确认是否删除？", function() {
						/* Public.ajaxPost("category/deleteType", {
							id: rowId
						}, function(data) {
							alert(data);
							$("#grid1").jqGrid().trigger("reloadGrid");
						}); */
						$.ajax({
							url:encodeURI("/delUserRole"),
							type:"POST",
							data:{
								"companyname":companyname,
								"rolecode":rolecode,
								"usercode":usercode
							},
							success:function(data){
								$("#grid1").jqGrid().trigger("reloadGrid");	
							}
						});
					});
				},
				callback: function() {
					$("#grid1").jqGrid().trigger("reloadGrid");
				}
			};
	</script>
</body>
</html>
